package com.cldz.service;

import com.cldz.common.dto.res.FsuAlarmResDto;
import com.cldz.dao.FsuAlarmMapper;
import com.cldz.dao.UserPropertyMapper;
import com.cldz.utils.BeanCopyUtil;
import com.cldz.utils.MathUtils;
import com.cldz.common.dto.req.FsuAlarmDto;
import com.cldz.entity.FsuAlarm;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ Author     ：lixiao.
 * @ Date       ：Created in 13:48 2018/12/22
 * @ Description：
 * @ Modified By：
 */
@Service("fsuAlarmService")
public class FsuAlarmService {

    @Autowired
    private FsuAlarmMapper fsuAlarmMapper;

    @Autowired
    private UserPropertyMapper userPropertyMapper;


    public List<FsuAlarmResDto> getFsuSuIDRunAlarmData(FsuAlarmDto fsuAlarmDto){
        List<FsuAlarm> fsuSuIDRunAlarmData = fsuAlarmMapper.getFsuSuIDRunAlarmData(fsuAlarmDto);
        return copyListBean(fsuSuIDRunAlarmData);
    }

    public Integer getFsuSuIDRunAlarmDataAmount(FsuAlarmDto fsuAlarmDto){

        return fsuAlarmMapper.getFsuSuIDRunAlarmDataAmount(fsuAlarmDto);
    }

    public List<FsuAlarmResDto> getFsuSuIDHisAlarmData(FsuAlarmDto fsuAlarmDto){
        List<FsuAlarm> fsuSuIDRunAlarmData = fsuAlarmMapper.getFsuSuIDHisAlarmData(fsuAlarmDto);
        return copyListBean(fsuSuIDRunAlarmData);
    }

    public Integer getFsuSuIDHisAlarmDataAmount(FsuAlarmDto fsuAlarmDto){

        return fsuAlarmMapper.getFsuSuIDHisAlarmDataAmount(fsuAlarmDto);
    }

    public List<FsuAlarmResDto> getFsuAllRunAlarmData(FsuAlarmDto fsuAlarmDto) {
        setAreaID(fsuAlarmDto);
        List<FsuAlarm> list = fsuAlarmMapper.getFsuAllRunAlarmData(fsuAlarmDto);
        return copyListBean(list);
    }

    public Integer getFsuAllRunAlarmDataAmount(FsuAlarmDto fsuAlarmDto) {
        setAreaID(fsuAlarmDto);
        return fsuAlarmMapper.getFsuAllRunAlarmDataAmount(fsuAlarmDto);
    }

    public List<FsuAlarmResDto> getFsuHisAlarmData(FsuAlarmDto fsuAlarmDto) {
        List<FsuAlarm> list = fsuAlarmMapper.getFsuHisAlarmData(fsuAlarmDto);
        return copyListBean(list);
    }

    public Integer getFsuHisAlarmDataAmount(FsuAlarmDto fsuAlarmDto) {

        return fsuAlarmMapper.getFsuHisAlarmDataAmount(fsuAlarmDto);
    }

    public List<Map<String, Object>> getFsuRunAlarmCount(FsuAlarmDto fsuAlarmDto) {
        String suIDs = fsuAlarmDto.getSuIDs();
        List<Map<String,Object>> list = new ArrayList<>();
        if(StringUtils.isNotBlank(suIDs)){
            String[] split = StringUtils.split(suIDs, ",");
            for (String suID:split) {
                Map<String,Object> map = new HashMap<>();
                Integer amount = fsuAlarmMapper.getFsuRunAlarmCount(suID);
                map.put("suID",suID);
                map.put("count",amount);
                list.add(map);
            }
        }

        return list;
    }



    private List<FsuAlarmResDto> copyListBean(List<FsuAlarm> devListByTypeID) {
        List<FsuAlarmResDto> list = new ArrayList<>();
        for (FsuAlarm device : devListByTypeID) {
            FsuAlarmResDto dto = new FsuAlarmResDto();
            BeanCopyUtil.copyPropertiesIgnoreNull(device, dto);
//            BeanUtils.copyProperties(device, dto);
            list.add(dto);
        }
        return list;
    }

    private void setAreaID(FsuAlarmDto fsuAlarmDto) {
        Integer areaID = fsuAlarmDto.getAreaID();
        if (areaID == null) {
            areaID = userPropertyMapper.queryAreaIDByUserID(fsuAlarmDto.getUserID());
        }
        fsuAlarmDto.setAreaID(MathUtils.getAreaIDPrefix(areaID));
    }


    public List<Map<String, Object>> getFsuHisAlarmCount(FsuAlarmDto fsuAlarmDto) {
        String suIDs = fsuAlarmDto.getSuIDs();
        List<Map<String,Object>> list = new ArrayList<>();
        if(StringUtils.isNotBlank(suIDs)){
            String[] split = StringUtils.split(suIDs, ",");
            for (String suID:split) {
                Map<String,Object> map = new HashMap<>();
                Integer amount = fsuAlarmMapper.getFsuHisAlarmCount(suID);
                map.put("suID",suID);
                map.put("count",amount);
                list.add(map);
            }
        }
        return list;
    }
}
